var read;
$(function () {
    read = $.cookie('read');
    console.log(read);
    if(read == null || read == undefined || read == ''){
        read = {};
    }else{
        read = JSON.parse(read);
    }
    $("#jqGrid").jqGrid({
        url: baseURL + 'facejava/javaquestion/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true},
            {label: '等级', name: 'level', index: 'level', width: 50,formatter:function(cellvalue, options, rowdata){
                    if(cellvalue < 3){
                        return "<span style='color: green'>"+cellvalue+"星</span>";
                    }
                    if(cellvalue == 3){
                        return "<span style='color: yellow'>"+cellvalue+"星</span>";
                    }
                    if(cellvalue > 3){
                        return "<span style='color: red;font-weight: 800'>"+cellvalue+"星</span>";
                    }
                }
            },
            {label: '提问', name: 'question', index: 'question', width: 180},
            {label: '类型', name: 'type', index: 'type', width: 180},
            {label: '创建日期', name: 'createTime', index: 'create_time', width: 180},
            {label: '最近30天是否读过', name: 'is_read', index: 'is_read',align:"center", width: 90,formatter: function (cellvalue, options, rowdata) {
                   if(read.hasOwnProperty(rowdata.id)){
                       return "<span style='color: green;font-weight: 800'>是</span>";
                   }else{
                       return "否";
                   }
                }},
            {
                label: '操作', name: 'do',align:"center", index: 'do', width: 80, formatter: function (cellvalue, options, rowdata) {
                    return '<button id="fat-btn" class="btn btn-primary" onclick="showAnswer('+rowdata.id+')" type="button">查看答案</button>';
                }
            }
        ],
        viewrecords: true,
        height: 600,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        autoScroll: true,
        width:"100%",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            //$("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

function showAnswer(id){
    $.ajax({
        type: "POST",
        url: baseURL + "facejava/javaquestion/info/" + id,
        contentType: "application/json",
        success: function (r) {
            if(r!= null){
                vm.$data.answer=r.javaQuestion.answer;
                layer.open({
                    type: 1,
                    closeBtn: false,
                    shift: 2,
                    shadeClose: true,
                    resize:true,
                    area: ['1000px', '700px'],
                    content: $("#layerBox")
                });
                read[id] = id;
                $.cookie('read', JSON.stringify(read), { expires: 30 });
            }else{
                layer.msg("答案消失了");
            }
        },
        error:function(){
            layer.msg("网络错误");
        }
    });
}

function levelBtn(level) {
    var ids = getSelectedRows();
    if (ids == null) {
        return;
    }
    confirm('确定要评分选中的记录？', function () {
        $.ajax({
            type: "POST",
            url: baseURL + "facejava/javaquestion/level?level=" + level,
            contentType: "application/json",
            data: JSON.stringify(ids),
            success: function (r) {
                if (r.code == 0) {
                    vm.search();
                    alert('操作成功', function () {
                        layer.close(index);
                    });
                } else {
                    alert(r.msg);
                }
            }
        });
    });
}
var index;
var vm = new Vue({
    el: '#rrapp',
    data:{
        showList: true,
        title: null,
        answer:"1111",
        javaQuestion: {}
    } ,
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.javaQuestion = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            var url = vm.javaQuestion.id == null ? "facejava/javaquestion/save" : "facejava/javaquestion/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.javaQuestion),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: baseURL + "facejava/javaquestion/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "facejava/javaquestion/info/" + id, function (r) {
                vm.javaQuestion = r.javaQuestion;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        },
        search: function (event) {
            $("#jqGrid").setGridParam( //G,P要大写
                {
                    url: baseURL + 'facejava/javaquestion/list',
                    page: 1,
                    postData: {
                        type: $("#typeSearch").val(),
                        pointS: $("#levelSearch1").val(),
                        pointE: $("#levelSearch2").val(),
                        keyword: $("#keyword").val()
                    }
                }
            ).trigger("reloadGrid");
        },
        level: function (event) {
            index = layer.open({
                content: "请选择星级(越高代表越重要)",
                area: ["500px"],
                btn: ['0星', '1星', '2星', '3星', '4星', '5星']
                , btn1: function (index, layero) {
                    levelBtn(0);
                    return false;
                }
                , btn2: function (index, layero) {
                    levelBtn(1);
                    return false;
                }
                , btn3: function (index, layero) {
                    levelBtn(2);
                    return false;
                }
                , btn4: function (index, layero) {
                    levelBtn(3);
                    return false;
                }
                , btn5: function (index, layero) {
                    levelBtn(4);
                    return false;
                }, btn6: function (index, layero) {
                    levelBtn(5);
                    return false;
                }

            });
        }
    }
});